using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FastDev;
public class SitDownAction : GoapAction
{
    public override string Name { get; protected set; } = "SitDown";
    public override int Cost { get; protected set; } = 5;
    public override GoapState PreCondition { get; protected set; } = new GoapState(new Dictionary<string, int>() {
        { AIStateKey2.HasChair,1 },
    });
    public override GoapState Effect { get; protected set; } = new GoapState(new Dictionary<string, int>()
    {
        {AIStateKey2.SitDown,1 },
    });
    public override GoapAgent Agent { get; protected set; }
    public GameObject Target { get; protected set; }

    public SitDownAction(GoapAgent goapAgent) : base(goapAgent)
    {

    }


    public override void OnStart()
    {
        base.OnStart();

        Chair[] chairs = GameObject.FindObjectsOfType<Chair>();

        foreach (var item in chairs)
        {
            if (!item.IsHasPeople)
            {
                Target = item.gameObject;
                ((AIAgent)Agent).Chair = item;
                ((AIAgent)Agent).Chair.IsHasPeople = true;
                break;
            }
        }

        if (Target == null)
        {
            Agent.OnActionFailed(this);
        }
    }

    public override void OnUpdate()
    {
        base.OnUpdate();


        if (Vector3.Distance(Target.transform.position, Agent.transform.position) > 1)
        {
            Agent.transform.Translate((Target.transform.position - Agent.transform.position).normalized * 10 * Time.deltaTime);
            return;
        }
        Agent.GoapState.SetValue(AIStateKey2.SitDown, 1);
    }

}
